package mission2.model;

import java.util.ArrayList;

public class toTree {

	private BTree<Elements> tree = new BTree<Elements>();
	
	public static void listToTree(ArrayList<Elements> objectList) {
		
		// Appelle a la fonction avec accumulateur
		listToTreeAcc(objectList, 0, 0);
	}
	
	public static BTNode<Elements> listToTreeAcc(ArrayList<Elements> objectList, int i, int par) {
		Elements e = objectList.get(i);
		String type = e.getClass().getSimpleName();
		
		if(type.equals("OpParL")) {
			listToTreeAcc(objectList, i++, par++);
		}
		else if(type.equals("OpParR")) {
			listToTreeAcc(objectList, i++, par--);
		}
		
		if(par == 0 && (objectList.size()-1) > i) {
			
			ArrayList<Elements> left = (ArrayList<Elements>) objectList.subList(1, i-1);
			ArrayList<Elements> right = (ArrayList<Elements>) objectList.subList(i+1, objectList.size()-1);
			
			return new BTNode<Elements>(e, listToTreeAcc(left, 0, 0), listToTreeAcc(right, 0, 0), null);
		}
	}
}